<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Compiler Functions</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="plugins.html" title="Chapter 18. Extending Smarty With Plugins">
<link rel="prev" href="plugins.block.functions.html" title="Block Functions">
<link rel="next" href="plugins.prefilters.postfilters.html" title="Prefilters/Postfilters">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Compiler Functions</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="plugins.block.functions.html">Prev</a> </td>
<th width="60%" align="center">Chapter 18. Extending Smarty With Plugins</th>
<td width="20%" align="right"> <a accesskey="n" href="plugins.prefilters.postfilters.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Compiler Functions">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="plugins.compiler.functions"></a>Compiler Functions</h2></div></div></div>
<p>
     Compiler functions are called only during compilation of the template.
     They are useful for injecting PHP code or time-sensitive static
     content into the template. If there is both a compiler function and a
     <a class="link" href="language.custom.functions.html" title="Chapter 8. Custom Functions">custom function</a> registered
      under the same name, the compiler function has precedence.
    </p>
<div class="funcsynopsis">
<table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table">
<tr>
<td><code class="funcdef">mixed <b class="fsfunc">smarty_compiler_name</b>(</code></td>
<td>
<var class="pdparam">$params</var>, </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<var class="pdparam">$smarty</var><code>)</code>;</td>
<td> </td>
</tr>
</table>
<div class="paramdef-list">
<code>array <var class="pdparam">$params</var></code>;<br><code>object <var class="pdparam">$smarty</var></code>;</div>
<div class="funcprototype-spacer"> </div>
</div>
<p>
     The compiler function is passed two parameters: the params array which contains precompiled
     strings for the attribute values and the Smarty object. It's supposed to return the code
     to be injected into the compiled template including the surrounding PHP tags.
    </p>
<div class="example">
<a name="id464861"></a><p class="title"><b>Example 18.6. A simple compiler function</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     compiler.tplheader.php
 * Type:     compiler
 * Name:     tplheader
 * Purpose:  Output header containing the source file name and
 *           the time it was compiled.
 * -------------------------------------------------------------
 */
function smarty_compiler_tplheader($params, Smarty $smarty)
{
    return "&lt;?php\necho '" . $smarty-&gt;_current_file . " compiled at " . date('Y-m-d H:M'). "';\n?&gt;";
}
?&gt;

</pre>
<p>
      This function can be called from the template as:
     </p>
<pre class="programlisting">

{* this function gets executed at compile time only *}
{tplheader}

     </pre>
<p>
      The resulting PHP code in the compiled template would be something like this:
     </p>
<pre class="programlisting">

&lt;?php
echo 'index.tpl compiled at 2002-02-20 20:02';
?&gt;

     </pre>
</div>
</div>
<br class="example-break"><p>
     See also
     <a class="link" href="api.register.plugin.html" title="registerPlugin()"><code class="varname">registerPlugin()</code></a>,
     <a class="link" href="api.unregister.plugin.html" title="unregisterPlugin"><code class="varname">unregisterPlugin()</code></a>.
    </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="plugins.block.functions.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="plugins.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="plugins.prefilters.postfilters.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Block Functions </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Prefilters/Postfilters</td>
</tr>
</table>
</div>
</body>
</html>
